﻿ options nolabel;

proc import out=work.test16 datafile="I:/prepare/pseudo_data.dta" dbms=dta replace;run;  
proc import out=work.crypto_factor datafile="I:/prepare/crypto_factor.dta" dbms=dta replace;run;  

data umd; set test16;if year= 2019 or (year=2018 and month>=8) or year=2020 or  year=2021;run;
proc means data=umd; var ab_turn_daily_new_30; run;

proc sort data=umd; by date; run;
proc rank data=umd out=umd2 group=5;
  by date;
    var ab_turn_daily_new_30;
    ranks momr;
run;
proc sort data=umd2; by momr; run;
proc sort data=umd2 ; by date momr ; run;

proc means data = umd2 noprint;
  by date momr;
    var holding_ret_daily; /*If DGTW, use holding_ret_daily_new --> Table "excess return"*/
      output out = ewretdat mean= ewret std = ewretstd;
run;

data ewretdat1; set ewretdat; 
momr=momr+1;
if missing(momr) then delete;
rank=momr;
rank_id=cats('PORT',rank);
 run;

proc sort data=ewretdat1; by date rank; run;
proc transpose data=ewretdat1 out=ewretdat2 ;

by date;id rank_id;var ewret;
run;


Data gout; set ewretdat2;ret_diff=PORT5-PORT1;

holding_date = intnx("day",date, 1,"e");
format holding_Date MMDDYY10.;
run;

proc means data=gout n mean t probt std;var PORT1 PORT2 PORT3 PORT4 PORT5 ret_diff ;run;

proc sql;
	create table gout2 as select a.*, b.cmkt, b.csmb, b.cmom, b.rf_daily
		from gout as a left join crypto_factor as b on 
		a.holding_date=b.date_temp;
	run;

data ff4; set gout2;     run;

data ff4; set ff4;

/*For returns (Panel A), use the following codes.*/

PORT1_rf=PORT1-rf_daily;
PORT2_rf=PORT2-rf_daily;
PORT3_rf=PORT3-rf_daily;
PORT4_rf=PORT4-rf_daily;
PORT5_rf=PORT5-rf_daily;

/*For non-return characteristics(Panel B), use the following codes --> Check Table "excess return"*/
/*
PORT1_rf=PORT1;
PORT2_rf=PORT2;
PORT3_rf=PORT3;
PORT4_rf=PORT4;
PORT5_rf=PORT5;
*/
ret_diff=port5_rf-port1_rf;
year=year(date);
month=month(date);
run;

proc means data=ff4 n mean t probt std;var PORT1_rf PORT2_rf PORT3_rf PORT4_rf PORT5_Rf ret_diff; run;


%let lag=8;


proc model data=ff4; parms a1 ;exogenous ret_diff ; instruments / intonly;ret_diff= a1 ; fit ret_diff / gmm kernel=(bart,%eval(&lag+1),0);ods output parameterestimates=ret_diff_Excess_ret  fitstatistics=fitresult OutputStatistics=residual;
data ret_diff_Excess_ret (keep=port estimate stderr tvalue probt df );set ret_diff_Excess_ret; port='ret_diff_3factor'; if parameter='a1'; run;

proc model data=ff4; parms a1  ;exogenous PORT1_rf; instruments / intonly;PORT1_rf= a1 ; fit PORT1_rf / gmm kernel=(bart,%eval(&lag+1), 0);ods output parameterestimates=PORT1_rf_Excess_ret  fitstatistics=fitresult OutputStatistics=residual;
data PORT1_rf_Excess_ret (keep=port estimate stderr tvalue probt df );set PORT1_rf_Excess_ret; port='PORT1_rf_3factor'; if parameter='a1'; run;

proc model data=ff4; parms a1  ;exogenous PORT2_rf ; instruments / intonly;PORT2_rf= a1 ; fit PORT2_rf / gmm kernel=(bart, %eval(&lag+1), 0);ods output parameterestimates=PORT2_rf_Excess_ret  fitstatistics=fitresult OutputStatistics=residual;
data PORT2_rf_Excess_ret (keep=port estimate stderr tvalue probt df );set PORT2_rf_Excess_ret; port='PORT2_rf_3factor'; if parameter='a1'; run;

proc model data=ff4; parms a1  ;exogenous PORT3_rf ; instruments / intonly;PORT3_rf= a1; fit PORT3_rf / gmm kernel=(bart, %eval(&lag+1), 0);ods output parameterestimates=PORT3_rf_Excess_ret  fitstatistics=fitresult OutputStatistics=residual;
data PORT3_rf_Excess_ret (keep=port estimate stderr tvalue probt df );set PORT3_rf_Excess_ret; port='PORT3_rf_3factor'; if parameter='a1'; run;

proc model data=ff4; parms a1  ;exogenous PORT4_rf; instruments / intonly;PORT4_rf= a1 ; fit PORT4_rf / gmm kernel=(bart, %eval(&lag+1), 0);ods output parameterestimates=PORT4_rf_Excess_ret  fitstatistics=fitresult OutputStatistics=residual;
data PORT4_rf_Excess_ret (keep=port estimate stderr tvalue probt df );set PORT4_rf_Excess_ret; port='PORT4_rf_3factor'; if parameter='a1'; run;

proc model data=ff4; parms a1  ;exogenous PORT5_rf ; instruments / intonly;PORT5_rf= a1 ; fit PORT5_rf / gmm kernel=(bart, %eval(&lag+1), 0);ods output parameterestimates=PORT5_rf_Excess_ret fitstatistics=fitresult OutputStatistics=residual;
data PORT5_rf_Excess_ret (keep=port estimate stderr tvalue probt df );set PORT5_rf_Excess_ret; port='PORT5_rf_3factor'; if parameter='a1'; run;

data Excess_ret; 
set 
PORT1_rf_Excess_ret
PORT2_rf_Excess_ret
PORT3_rf_Excess_ret
PORT4_rf_Excess_ret
PORT5_rf_Excess_ret
ret_diff_Excess_ret;
run;

proc model data=ff4; parms a1 b1 ;instruments cmkt ;ret_diff= a1 + b1*cmkt; fit ret_diff / gmm kernel=(bart,%eval(&lag+1), 0);ods output parameterestimates=ret_diff_CAPM  fitstatistics=fitresult OutputStatistics=residual;
data ret_diff_CAPM (keep=port estimate stderr tvalue probt df );set ret_diff_CAPM; port='ret_diff_3factor'; if parameter='a1'; run;

proc model data=ff4; parms a1 b1 ;instruments cmkt ;PORT1_rf= a1 + b1*cmkt; fit PORT1_rf / gmm kernel=(bart,%eval(&lag+1), 0);ods output parameterestimates=PORT1_rf_CAPM  fitstatistics=fitresult OutputStatistics=residual;
data PORT1_rf_CAPM (keep=port estimate stderr tvalue probt df );set PORT1_rf_CAPM; port='PORT1_rf_3factor'; if parameter='a1'; run;

proc model data=ff4; parms a1 b1 ;instruments cmkt ;PORT2_rf= a1 + b1*cmkt; fit PORT2_rf / gmm kernel=(bart, %eval(&lag+1), 0);ods output parameterestimates=PORT2_rf_CAPM  fitstatistics=fitresult OutputStatistics=residual;
data PORT2_rf_CAPM (keep=port estimate stderr tvalue probt df );set PORT2_rf_CAPM; port='PORT2_rf_3factor'; if parameter='a1'; run;

proc model data=ff4; parms a1 b1 ;instruments cmkt ;PORT3_rf= a1 + b1*cmkt; fit PORT3_rf / gmm kernel=(bart, %eval(&lag+1), 0);ods output parameterestimates=PORT3_rf_CAPM  fitstatistics=fitresult OutputStatistics=residual;
data PORT3_rf_CAPM (keep=port estimate stderr tvalue probt df );set PORT3_rf_CAPM; port='PORT3_rf_3factor'; if parameter='a1'; run;

proc model data=ff4; parms a1 b1 ;instruments cmkt ;PORT4_rf= a1 + b1*cmkt; fit PORT4_rf / gmm kernel=(bart,%eval(&lag+1), 0);ods output parameterestimates=PORT4_rf_CAPM  fitstatistics=fitresult OutputStatistics=residual;
data PORT4_rf_CAPM (keep=port estimate stderr tvalue probt df );set PORT4_rf_CAPM; port='PORT4_rf_3factor'; if parameter='a1'; run;

proc model data=ff4; parms a1 b1 ;instruments cmkt ;PORT5_rf= a1 + b1*cmkt; fit PORT5_rf/ gmm kernel=(bart, %eval(&lag+1), 0);ods output parameterestimates=PORT5_rf_CAPM  fitstatistics=fitresult OutputStatistics=residual;
data PORT5_rf_CAPM (keep=port estimate stderr tvalue probt df );set PORT5_rf_CAPM; port='PORT5_rf_3factor'; if parameter='a1'; run;


data CAPM; 
set 
PORT1_rf_CAPM
PORT2_rf_CAPM
PORT3_rf_CAPM
PORT4_rf_CAPM
PORT5_rf_CAPM

ret_diff_CAPM;
run;




proc model data=ff4; parms a1 b1 b2 b3 ;instruments cmkt csmb cmom;PORT1_rf= a1 +b1*cmkt  + b2*csmb + b3*cmom; fit PORT1_rf / gmm kernel=(bart, %eval(&lag+1), 0);ods output parameterestimates=PORT1_rf_3factor  fitstatistics=fitresult OutputStatistics=residual;
data PORT1_rf_3factor (keep=port estimate stderr tvalue probt df );set PORT1_rf_3factor; port='PORT1_rf_3factor'; if parameter='a1'; run;

proc model data=ff4; parms a1 b1 b2 b3;instruments cmkt csmb cmom;PORT2_rf= a1 + b1*cmkt  + b2*csmb + b3*cmom; fit PORT2_rf / gmm kernel=(bart,%eval(&lag+1), 0);ods output parameterestimates=PORT2_rf_3factor  fitstatistics=fitresult OutputStatistics=residual;
data PORT2_rf_3factor (keep=port estimate stderr tvalue probt df );set PORT2_rf_3factor; port='PORT2_rf_3factor'; if parameter='a1'; run;

proc model data=ff4; parms a1 b1 b2 b3 ;instruments cmkt csmb cmom;PORT3_rf= a1 + b1*cmkt  + b2*csmb + b3*cmom; fit PORT3_rf / gmm kernel=(bart, %eval(&lag+1), 0);ods output parameterestimates=PORT3_rf_3factor  fitstatistics=fitresult OutputStatistics=residual;
data PORT3_rf_3factor (keep=port estimate stderr tvalue probt df );set PORT3_rf_3factor; port='PORT3_rf_3factor'; if parameter='a1'; run;

proc model data=ff4; parms a1 b1 b2 b3 ;instruments cmkt csmb cmom;PORT4_rf= a1 + b1*cmkt  + b2*csmb + b3*cmom; fit PORT4_rf / gmm kernel=(bart, %eval(&lag+1), 0);ods output parameterestimates=PORT4_rf_3factor  fitstatistics=fitresult OutputStatistics=residual;
data PORT4_rf_3factor (keep=port estimate stderr tvalue probt df );set PORT4_rf_3factor; port='PORT4_rf_3factor'; if parameter='a1'; run;

proc model data=ff4; parms a1 b1 b2 b3 ;instruments cmkt csmb cmom ;PORT5_rf= a1 + b1*cmkt  + b2*csmb + b3*cmom; fit PORT5_rf / gmm kernel=(bart, %eval(&lag+1), 0);ods output parameterestimates=PORT5_rf_3factor  fitstatistics=fitresult OutputStatistics=residual;
data PORT5_rf_3factor (keep=port estimate stderr tvalue probt df );set PORT5_rf_3factor; port='PORT5_rf_3factor'; if parameter='a1'; run;

proc model data=ff4; parms a1 b1 b2 b3 ;instruments cmkt csmb cmom;ret_diff= a1 + b1*cmkt  + b2*csmb + b3*cmom; fit ret_diff / gmm kernel=(bart, %eval(&lag+1), 0);ods output parameterestimates=ret_diff_3factor  fitstatistics=fitresult OutputStatistics=residual;
data ret_diff_3factor (keep=port estimate stderr tvalue probt df );set ret_diff_3factor; port='ret_diff_3factor'; if parameter='a1'; run;


data threefactor; 
set 
PORT1_rf_3factor
PORT2_rf_3factor
PORT3_rf_3factor
PORT4_rf_3factor
PORT5_rf_3factor

ret_diff_3factor;
run;
